//
//  Player.m
//  ToWhere
//
//  Created by dllo on 15/11/23.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "Player.h"
#import <STKAudioPlayer.h>
#import "MusicDetailModel.h"

typedef enum
{
    OrderModel = 1,
    randomModel,
    OneSongMode ,
}Model;

@interface Player()<STKAudioPlayerDelegate>

@property(nonatomic, retain) STKAudioPlayer *player;

@end

@implementation Player

+(Player *)sharePlayer
{
    static Player *player = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        player = [[Player alloc]init];
    });
    return player;
}

-(instancetype)init
{
    self.player = [[STKAudioPlayer alloc]initWithOptions:(STKAudioPlayerOptions){
        .flushQueueOnSeek = YES,
        .enableVolumeMixer = YES
    }];
    self.player.delegate = self;
    return self;
}

-(void)playMusic:(NSString *)playUrl
{
    [self.player play:playUrl];
}

-(void)playStop
{
    [self.player pause];
}

//播放暂停
- (void)playBtnAction:(UIButton *)button{
    
    //暂停
    if (self.player.state == STKAudioPlayerStatePaused) {
        [button setBackgroundImage:[UIImage imageNamed:@"Music_kaishi"] forState:UIControlStateNormal];
        //继续播放
        [self.player resume];
        
        //当前是播放状态
    }else if (self.player.state == STKAudioPlayerStatePlaying){
        
        [button setBackgroundImage:[UIImage imageNamed:@"Music_zanting"] forState:UIControlStateNormal];
        [self.player pause];
        //其他
    }else{
//        [self playMusic];
//        [self.playBtn setTitle:@"暂停" forState:UIControlStateNormal];
    }
}

//下一曲
- (void)nextBtnAction:(UIButton *)Button playModel:(NSInteger)playModel
{
      if (playModel == OrderModel) {
            self.row++;
            if (self.row == self.playListArray.count) {
                self.row = 0;
                MusicDetailModel *model = self.playListArray[self.row];
                [self.player play:model.source];
        }
            MusicDetailModel *model = self.playListArray[self.row];
            [self.player play:model.source];
            
        }
        if (playModel == randomModel) {
            self.row = arc4random() % self.playListArray.count;
            MusicDetailModel *model = self.playListArray[self.row];
            [self.player play:model.source];
        }
        if (playModel == OneSongMode) {
            MusicDetailModel *model = self.playListArray[self.row];
            [self.player play:model.source];
        }
   
}

//上一曲
-(void)upBtnAction:(UIButton *)button playModel:(NSInteger)playModel
{
    if (playModel == OrderModel) {
        self.row--;
        if (self.row == 0) {
            self.row = 0;
            MusicDetailModel *model = self.playListArray[self.row];
            [self.player play:model.source];
        }
        MusicDetailModel *model = self.playListArray[self.row];
        [self.player play:model.source];
        
    }
    if (playModel == randomModel) {
        self.row = arc4random() % self.playListArray.count;
        MusicDetailModel *model = self.playListArray[self.row];
        [self.player play:model.source];
    }
    if (playModel == OneSongMode) {
        MusicDetailModel *model = self.playListArray[self.row];
        [self.player play:model.source];
    }
 
}

//判断当期播放状态
-(BOOL)playStatus
{
    if (self.player.state == STKAudioPlayerStatePlaying) {
        return YES;
    }
    else
    {
        return NO;
    }
}


- (void)audioPlayer:(STKAudioPlayer *)audioPlayer didStartPlayingQueueItemId:(NSObject *)queueItemId{
    NSLog(@"开始播放");
}

- (void)audioPlayer:(STKAudioPlayer *)audioPlayer stateChanged:(STKAudioPlayerState)state previousState:(STKAudioPlayerState)previousState{
    NSLog(@"状态改变%u",previousState);
}

- (void)audioPlayer:(STKAudioPlayer *)audioPlayer didCancelQueuedItems:(NSArray *)queuedItems{
    NSLog(@"未知%@",queuedItems);
}

- (void)audioPlayer:(STKAudioPlayer *)audioPlayer didFinishBufferingSourceWithQueueItemId:(NSObject *)queueItemId{
    NSLog(@"完成加载");
}

- (void)audioPlayer:(STKAudioPlayer *)audioPlayer logInfo:(NSString *)line{
    NSLog(@"信息%@",line);
}

- (void)audioPlayer:(STKAudioPlayer *)audioPlayer unexpectedError:(STKAudioPlayerErrorCode)errorCode{
    NSLog(@"%u错误",errorCode);
}

- (void)audioPlayer:(STKAudioPlayer *)audioPlayer didFinishPlayingQueueItemId:(NSObject *)queueItemId withReason:(STKAudioPlayerStopReason)stopReason andProgress:(double)progress andDuration:(double)duration{
    NSLog(@"结束播放");
}


@end
